import CryptoJS from 'crypto-js'
import { JSEncrypt } from 'jsencrypt'

/**
 * aes加密
 */
export const aesUtil = {

    // 获取key
    genKey: function(length = 16) {
        const random = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
        let str = ''
        for (let i = 0; i < length; i++) {
            str = str + random.charAt(Math.random() * random.length)
        }
        return str
    },

    // 加密
    encrypt: function(plaintext, key) {
        if (plaintext instanceof Object) {
            // JSON.stringify
            plaintext = JSON.stringify(plaintext)
        }
        const encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(plaintext), CryptoJS.enc.Utf8.parse(key), {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return encrypted.toString()
    },

    // 解密
    decrypt: function(ciphertext, key) {
        const decrypt = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        let decString = CryptoJS.enc.Utf8.stringify(decrypt).toString()
        if (decString.charAt(0) === '{' || decString.charAt(0) === '[') {
            // JSON.parse
            decString = JSON.parse(decString)
        }
        return decString
    }
}

/**
 * rsa加密
 */
export const rsaUtil = {
    // RSA 位数，这里要跟后端对应
    bits: 1024,

    // 当前JSEncrypted对象
    thisKeyPair: {},

    // 生成密钥对(公钥和私钥)
    genKeyPair: function(bits = rsaUtil.bits) {
        const genKeyPair = {}
        rsaUtil.thisKeyPair = new JSEncrypt({
            default_key_size: bits
        })

        // 获取私钥
        genKeyPair.privateKey = rsaUtil.thisKeyPair.getPrivateKey()

        // 获取公钥
        genKeyPair.publicKey = rsaUtil.thisKeyPair.getPublicKey()

        return genKeyPair
    },

    // 公钥加密
    encrypt: function(plaintext, publicKey) {
        // 由于秘钥已经生存一对保存在本地，该方法就在内部调用，生成setPublicKey方法
        this.genKeyPair()

        if (plaintext instanceof Object) {
            // 1、JSON.stringify
            plaintext = JSON.stringify(plaintext)
        }
        publicKey && rsaUtil.thisKeyPair.setPublicKey(publicKey)
        return rsaUtil.thisKeyPair.encrypt(JSON.stringify(plaintext))
    },

    // 私钥解密
    decrypt: function(ciphertext, privateKey) {
        privateKey && rsaUtil.thisKeyPair.setPrivateKey(privateKey)
        let decString = rsaUtil.thisKeyPair.decrypt(ciphertext)
        if (decString.charAt(0) === '{' || decString.charAt(0) === '[') {
            // JSON.parse
            decString = JSON.parse(decString)
        }
        return decString
    }
}

/**
 * 本地rsa已经生成的秘钥
 */
export const privateKey = `MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIFlUv7IpxoYFfNixDfiKGOe0f0Tqk9EBQcY3qO8wyJ+dkHqIGpW5oEpWWBxfUglvctmqSaudh54sIhoREnbszpVVh+ViIPRkSkYk99eZB2cRBCQgUaLcXxA6ZuK/+10o1MeD0gd8UCLReRV/xVpYSVJl8spMRTtQ7ljgcrQ9gQtAgMBAAECgYBGGtJ7jdTTE2eZJNKwgExnpN2aYvQr2+tpa/T7+0IxxxhlH+cOFiTup6tv0C0TULdioy1T8sO2QKdMUElnRqv0UTPh1XMEiVppXFP22Kh9LQ721bTpFGvrfWD8zTPmDT+ml0/+ykmNiFMEC6WUKIRtisQJjkdRUBQXS1Emqi8T9QJBAM0nvMBuZdcKm+bdP/CW4CMRQYvcNknYmJnfp2xzFlUdf6+YRyPSIge+A+TEmKBsBzqKdgfk6AWdN0KUJ27MRHcCQQChdvXJz/4m1JmlwOVCTWOd5UR9k83+z/r3jDv09W6FiXmpd0x2S1TOCIp9K7G56ARGZdwjrX8x+UvZl0HWypl7AkEAgPPbVvGNne+TMzga+rU7SY+NLcZLcXtt4DqO2EI25WVXXx5OiRuKPGeZtvBEJ4H/wTJLIocluHGrzHOnnaNT3QJADTP8Dz+ZybUSx1IJrS1E2xF3U0l1y8NYA0wNTw+OtQ6zyx69OuMYESzznh6njsshJlyH7sbuIrj+8dEDijvQ5wJAbUufG90t3VIj4rlnGPtjjVh3vXh1Hb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